import 'package:flutter/material.dart';

class Dart_Constants_Variables_Page extends StatelessWidget {

  void _test() {
    //使用var定义变量;可以不指定初始值，默认为null;可以给var定义的变量赋不同类型的值;
    var num;
    print("!!!!!!!!!变量的默认是：$num");
    num = 123;
    print("!!!!!!!!!赋值int类型：$num");
    num = "xiaoming";
    print("!!!!!!!!!赋值String类型：$num");
    num = true;
    print("!!!!!!!!!赋值布尔类型类型：$num");

    //final声明变量只能赋值一次,并不要求赋的值一定是编译时常量，可以是常量也可以不是
    //    final运行时常量,在程序运行的时候赋值，赋值后值不再改变
    final age = 20;
//    age = 30; //报错 'age' a final variable, can only be set once.
    final name = num;

    //    const 要求在声明时初始化，并且赋值必需为编译时常量
    const height = 170;
//    height =175;//报错 Constant variables can't be assigned a value.
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Dart的常量和变量"),
        ),
        body: Container(
          color: Color.fromARGB(255, 255, 255, 255),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _test,
          child: Text("click"),
        ),
      ),
    );
  }
}